﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui.Article.StockLoc
{
    public partial class UiEditStockLocation : Form
    {
        private bool isCreatingNew = false;
        Controller c;
        int id = 0;

        public UiEditStockLocation(Controller c, string[] areas)
        {
            InitializeComponent();
            this.Text = "Ny lagerplats";
            box_area.Items.AddRange(areas);
            isCreatingNew = true;
            this.c = c;
            box_area.SelectedIndex = 0;
            txt_shelf.Text = "000";
            txt_level.Text = "00";
            txt_section.Text = "00";
            this.Show();
        }

        public UiEditStockLocation(Controller c, string[] areas, string[] values)
        {
            InitializeComponent();
            box_area.Items.AddRange(areas);
            isCreatingNew = false;
            this.c = c;
            if (values != null && values.Length == 5)
            {
                id = int.Parse(values[0]);
                box_area.SelectedIndex = box_area.Items.IndexOf(values[1]);
                txt_shelf.Text = values[2];
                txt_level.Text = values[3];
                txt_section.Text = values[4];
            }
            else
            {
                Popup.Error("Oväntat värde");
                this.Close();
            }

            this.Show();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(isCreatingNew)
                c.SaveNewStockLocation(box_area.SelectedItem.ToString().Substring(0, 2), txt_shelf.Text, txt_level.Text, txt_section.Text, this);
            else
                c.UpdateStockLocation(id, box_area.SelectedItem.ToString().Substring(0, 2), txt_shelf.Text, txt_level.Text, txt_section.Text, this);
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            Popup.FunctionNotImplemented();
        }
    }
}
